home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / usr_28.txt < prev    next >
Encoding:
Text File  |  2001-09-26  |  15.3 KB  |  419 lines

  1. *usr_28.txt*    For Vim version 6.0.  Last change: 2001 Sep 04
  2.  
  3.              VIM USER MANUAL - by Bram Moolenaar
  4.  
  5.                    Folding
  6.  
  7.  
  8. Structured text can be separated in sections.  And sections in sub-sections.
  9. Folding allows you to display a section as one line, providing an overview.
  10. This chapter explains the different ways this can be done.
  11.  
  12. |28.1|    What is folding?
  13. |28.2|    Manual folding
  14. |28.3|    Working with folds
  15. |28.4|    Saving and restoring folds
  16. |28.5|    Folding by indent
  17. |28.6|    Folding with markers
  18. |28.7|    Folding by syntax
  19. |28.8|    Folding by expression
  20. |28.9|    Folding unchanged lines
  21. |28.10| Which fold method to use?
  22.  
  23.      Next chapter: |usr_29.txt|  Moving through programs
  24.  Previous chapter: |usr_27.txt|  Search commands and patterns
  25. Table of contents: |usr_toc.txt|
  26.  
  27. ==============================================================================
  28. *28.1*    What is folding?
  29.  
  30. Folding is used to show a range of lines in the buffer as a single line on the
  31. screen.  Like a piece of paper which is folded to make it shorter:
  32.  
  33.     +------------------------+
  34.     | line 1         |
  35.     | line 2         |
  36.     | line 3         |
  37.     |_______________________ |
  38.     \             \
  39.      \________________________\
  40.      / folded lines          /
  41.     /________________________/
  42.     | line 12         |
  43.     | line 13         |
  44.     | line 14         |
  45.     +------------------------+
  46.  
  47. The text is still in the buffer, unchanged.  Only the way lines are displayed
  48. is affected by folding.
  49.  
  50. The advantage of folding is that you can get a better overview of the
  51. structure of text, by folding lines of a section and replacing it with a line
  52. that indicates that there is a section.
  53.  
  54. ==============================================================================
  55. *28.2*    Manual folding
  56.  
  57. Try it out: Position the cursor in a paragraph and type: >
  58.  
  59.     zfap
  60.  
  61. You will see that the paragraph is replaced by a highlighted line.  You have
  62. created a fold.  |zf| is an operator and |ap| a text object selection.  You
  63. can use the |zf| operator with any movement command to create a fold for the
  64. text that it moved over.  |zf| also works in Visual mode.
  65.  
  66. To view the text again, open the fold by typing: >
  67.  
  68.     zo
  69.  
  70. And you can close the fold again with: >
  71.  
  72.     zc
  73.  
  74. All the folding commands start with 'z'.  With some fantasy, this looks like a
  75. folded piece of paper, seen from the side.  The letter after the "z" has a
  76. mnemonic meaning to make it easier to remember the commands:
  77.  
  78.     zf    F-old creation
  79.     zo    O-pen a fold
  80.     zc    C-lose a fold
  81.  
  82. Folds can be nested: A region of text that contains folds can be folded
  83. again.  For example, you can fold each paragraph in this section, and then
  84. fold all the sections in this chapter.  Try it out.  You will notice that
  85. opening the fold for the whole chapter will restore the nested folds as they
  86. were, some may be open and some may be closed.
  87.  
  88. Suppose you have created several folds, and now want to view all the text.
  89. You could go to each fold and type "zo".  To do this faster, use this command: >
  90.  
  91.     zr
  92.  
  93. This will R-educe the folding.  The opposite is: >
  94.  
  95.     zm
  96.  
  97. This folds M-ore.  You can repeat "zr" and "zm" to open and close nested folds
  98. of several levels.
  99.  
  100. If you have nested several levels deep, you can open all of them with: >
  101.  
  102.     zR
  103.  
  104. This R-educes folds until there are none left.  And you can close all folds
  105. with: >
  106.  
  107.     zM
  108.  
  109. This folds M-ore and M-ore.
  110.  
  111. You can quickly disable the folding with the |zn| command.  Then |zN| brings
  112. back the folding as it was.  |zi| toggles between the two.  This is a useful
  113. way of working:
  114. - create folds to get overview on your file
  115. - move around to where you want to do your work
  116. - do |zi| to look at the text and edit it
  117. - do |zi| again to go back to moving around
  118.  
  119. More about manual folding in the reference manual: |fold-manual|
  120.  
  121. ==============================================================================
  122. *28.3*    Working with folds
  123.  
  124. When some folds are closed, movement commands like "j" and "k" move over a
  125. fold like it was a single, empty line.  This allows you to quickly move around
  126. over folded text.
  127.  
  128. You can yank, delete and put folds as if it was a single line.  This is very
  129. useful if you want to reorder functions in a program.  First make sure that
  130. each fold contains a whole function (or a bit less) by selecting the right
  131. 'foldmethod'.  Then delete the function with "dd", move the cursor and put it
  132. with "p".  If some lines of the function are above or below the fold, you can
  133. use Visual selection:
  134. - put the cursor on the first line to be moved
  135. - hit "V" to start Visual mode
  136. - put the cursor on the last line to be moved
  137. - hit "d" to delete the selected lines.
  138. - move the cursor to the new position and "p"ut the lines there.
  139.  
  140. It is sometimes difficult to see or remember where a fold is located, thus
  141. where a |zo| command would actually work.  To see the defined folds: >
  142.  
  143.     :set foldcolumn
  144.  
  145. This will show a small column on the left of the window to indicate folds.
  146. A "-" is shown at the start of each fold.  A number indicates the fold level.
  147.  
  148. To open all folds at the cursor line use |zO|.
  149. To close all folds at the cursor line use |zC|.
  150. To delete a fold at the cursor line use |zd|.
  151. To delete all folds at the cursor line use |zD|.
  152.  
  153. When in Insert mode, the fold at the cursor line is never closed.  That allows
  154. you to see what you type!
  155.  
  156. Folds are opened automatically when jumping around or moving the cursor left
  157. or right.  For example, the "0" command opens the fold under the cursor
  158. (if 'foldopen' contains "hor", which is the default).  The 'foldopen' option
  159. can be changed to open folds for specific commands.  If you want the line
  160. under the cursor always to be open, do this: >
  161.  
  162.     :set foldopen=all
  163.  
  164. Warning: You won't be able to move onto a closed fold then.  You might want to
  165. use this only temporarily and then set it back to the default: >
  166.  
  167.     :set foldopen&
  168.  
  169. You can make folds close automatically when you move out of it: >
  170.  
  171.     :set foldclose=all
  172.  
  173. This will re-apply 'foldlevel' to all folds that don't contain the cursor.
  174. You have to try it out if you like how this feels.  Use |zm| to fold more and
  175. |zr| to fold less (reduce folds).
  176.  
  177. The folding is local to the window.  This allows you to open two windows on
  178. the same buffer, one with folds and one without folds.  Or one with all folds
  179. closed and one with all folds open.
  180.  
  181. ==============================================================================
  182. *28.4*    Saving and restoring folds
  183.  
  184. When you abandon a file (starting to edit another one), the state of the folds
  185. is lost.  If you come back to the same file later, all manually opened and
  186. closed folds are back to their default.  When folds have been created
  187. manually, all folds are gone!  To save the folds use the |:mkview| command: >
  188.  
  189.     :mkview
  190.  
  191. This will store the settings and other things that influence the view on the
  192. file.  You can change what is stored with the 'viewoptions' option.
  193. When you come back to the same file later, you can load the view again: >
  194.  
  195.     :loadview
  196.  
  197. You can store up to ten views on one file.  For example, to save the current
  198. setup as the third view and load the second view: >
  199.  
  200.     :mkview 3
  201.     :loadview 2
  202.  
  203. Note that when you insert or delete lines the views might become invalid.
  204. Also check out the 'viewdir' option, which specifies where the views are
  205. stored.  You might want to delete old views now and then.
  206.  
  207. ==============================================================================
  208. *28.5*    Folding by indent
  209.  
  210. Defining folds with |zf| is a lot of work.  If your text is structured by
  211. giving lower level items a larger indent, you can use the indent folding
  212. method.  This will create folds for every sequence of lines with the same
  213. indent.  Lines with a larger indent will become nested folds.  This works well
  214. with many programming languages.
  215.  
  216. Try this by setting the 'foldmethod' option: >
  217.  
  218.     :set foldmethod=indent
  219.  
  220. Then you can use the |zm| and |zr| commands to fold more and reduce folding.
  221. It's easy to see on this example text:
  222.  
  223. This line is not indented
  224.     This line is indented once
  225.         This line is indented twice
  226.     This line is indented once
  227. This line is not indented
  228.     This line is indented once
  229.     This line is indented once
  230.  
  231. Note that the relation between the amount of indent and the fold depth depends
  232. on the 'shiftwidth' option.  Each 'shiftwidth' worth of indent adds one to the
  233. depth of the fold.  This is called a fold level.
  234.  
  235. When you use the |zr| and |zm| commands you actually increase or decrease the
  236. 'foldlevel' option.  You could also set it directly: >
  237.  
  238.     :set foldlevel=3
  239.  
  240. This means that all folds with three times a 'shiftwidth' indent or more will
  241. be closed.  The lower the foldlevel, the more folds will be closed.  When
  242. 'foldlevel' is zero, all folds are closed.  |zM| does set 'foldlevel' to zero.
  243. The opposite command |zR| sets 'foldlevel' to the deepest fold level that is
  244. present in the file.
  245.  
  246. Thus there are two ways to open and close the folds:
  247. (A) By setting the fold level.
  248.     This gives a very quick way of "zooming out" to view the structure of the
  249.     text, move the cursor, and "zoom in" on the text again.
  250.  
  251. (B) By using |zo| and |zc| commands to open or close specific folds.
  252.     This allows opening only those folds that you want to be open, while other
  253.     folds remain closed.
  254.  
  255. This can be combined: You can first close most folds by using |zm| a few times
  256. and then open a specific fold with |zo|.  Or open all folds with |zR| and
  257. then close specific folds with |zc|.
  258.  
  259. But you cannot manually define folds when 'foldmethod' is "indent", as that
  260. would conflict with the relation between the indent and the fold level.
  261.  
  262. More about folding by indent in the reference manual: |fold-indent|
  263.  
  264. ==============================================================================
  265. *28.6*    Folding with markers
  266.  
  267. Markers in the text are used to specify the start and end of a fold region.
  268. This gives precise control over which lines are included in a fold.  The
  269. disadvantage is that the text needs to be modified.
  270.  
  271. Try it: >
  272.  
  273.     :set foldmethod=marker
  274.  
  275. Example text, as it could appear in a C program:
  276.  
  277.     /* foobar () {{{ */
  278.     int foobar()
  279.     {
  280.         /* return a value {{{ */
  281.         return 42;
  282.         /* }}} */
  283.     }
  284.     /* }}} */
  285.  
  286. Notice that the folded line will display the text before the marker.  This is
  287. very useful to tell what the fold contains.
  288.  
  289. It's quite annoying when the markers don't pair up correctly after moving some
  290. lines around.  This can be avoided by using numbered markers.  Example:
  291.  
  292.     /* global variables {{{1 */
  293.     int varA, varB;
  294.  
  295.     /* functions {{{1 */
  296.     /* funcA() {{{2 */
  297.     void funcA() {}
  298.  
  299.     /* funcB() {{{2 */
  300.     void funcB() {}
  301.     /* }}}1 */
  302.  
  303. At every numbered marker a fold at the specified level begins.  This will make
  304. any fold at a higher level stop here.  You can just use numbered start markers
  305. to define all folds.  Only when you want to explicitly stop a fold before
  306. another starts you need to add an end marker.
  307.  
  308. More about folding with markers in the reference manual: |fold-marker|
  309.  
  310. ==============================================================================
  311. *28.7*    Folding by syntax
  312.  
  313. For each language Vim uses a different syntax file.  This defines the colors
  314. for various items in the file.  If you are reading this in Vim, in a terminal
  315. that supports colors, the colors you see are made with the "help" syntax file.
  316.    In the syntax files it is possible to add syntax items that have the "fold"
  317. argument.  These define a fold region.  This requires writing a syntax file
  318. and adding these items in it.  That's not so easy to do.  But once it's done,
  319. all folding happens automatically.
  320.    Here we'll assume you are using an existing syntax file.  Then there is
  321. nothing more to explain.  You can open and close folds as explained above.
  322. The folds will be created and deleted automatically when you edit the file.
  323.  
  324. More about folding by syntax in the reference manual: |fold-syntax|
  325.  
  326. ==============================================================================
  327. *28.8*    Folding by expression
  328.  
  329. This is similar to folding by indent, but instead of using the indent of a
  330. line a user function is called to compute the fold level of a line.  You can
  331. use this for text where something in the text indicates which lines belong
  332. together.  An example is an e-mail message where the quoted text is indicated
  333. by a ">" before the line.  To fold these quotes use this: >
  334.  
  335.     :set foldmethod=expr
  336.     :set foldexpr=strlen(substitute(substitute(getline(v:lnum),'\\s','',\"g\"),'[^>].*','',''))
  337.  
  338. You can try it out on this text:
  339.  
  340. > quoted text he wrote
  341. > quoted text he wrote
  342. > > double quoted text I wrote
  343. > > double quoted text I wrote
  344.  
  345. Explanation for the 'foldexpr' used in the example (inside out):
  346.    getline(v:lnum)            gets the current line
  347.    substitute(...,'\\s','','g')        removes all white space from the line
  348.    substitute(...,'[^>].*','',''))    removes everything after leading '>'s
  349.    strlen(...)                counts the length of the string, which
  350.                     is the number of '>'s found
  351.  
  352. Note that a backslash must be inserted before every space, double quote and
  353. backslash for the ":set" command.  If this confuses you, do >
  354.  
  355.     :set foldexpr
  356.  
  357. to check the actual resulting value.  To correct a complicated expression, use
  358. the command-line completion: >
  359.  
  360.     :set foldexpr=<Tab>
  361.  
  362. Where <Tab> is a real Tab.  Vim will fill in the previous value, which you can
  363. then edit.
  364.  
  365. More about folding by expression in the reference manual: |fold-expr|
  366.  
  367. ==============================================================================
  368. *28.9*    Folding unchanged lines
  369.  
  370. This is useful when you set the 'diff' option in the same window.  The
  371. |vimdiff| command does this for you.  Example: >
  372.  
  373.     setlocal diff foldmethod=diff scrollbind nowrap foldlevel=1
  374.  
  375. Do this in every window that shows a different version of the same file.  You
  376. will clearly see the differences between the files, while the text that didn't
  377. change is folded.
  378.  
  379. For more details see |fold-diff|.
  380.  
  381. ==============================================================================
  382. *28.10* Which fold method to use?
  383.  
  384. All these possibilities makes you wonder which method you should chose.
  385. Unfortunately, there is no golden rule.  Here are some hints.
  386.  
  387. If there is a syntax file with folding for the language you are editing, that
  388. is probably the best choice.  If there isn't one, you might try to write it.
  389. This requires a good knowledge of search patterns.  It's not easy, but when
  390. it's working you will not have to define folds manually.
  391.  
  392. Typing commands to manually fold regions can be used for unstructured text.
  393. Then use the |:mkview| command to save and restore your folds.
  394.  
  395. The marker method requires you to change the file.  If you are sharing the
  396. files with other people or you have to meet company standards, you might not
  397. be allowed to add them.
  398.    The main advantage of markers is that you can put them exactly where you
  399. want them.  That avoids that a few lines are missed when you cut and paste
  400. folds.  And you can add a comment about what is contained in the fold.
  401.  
  402. Folding by indent is something that works in many files, but not always very
  403. well.  Use it when you can't use one of the other methods.  However, it is
  404. very useful for outlining.  Then you specifically use one 'shiftwidth' for
  405. each nesting level.
  406.  
  407. Folding with expressions can make folds in almost any structured text.  It is
  408. quite simple to specify, especially if the start and end of a fold can easily
  409. be recognized.
  410.    If you use the "expr" method to define folds, but they are not exactly how
  411. you want them, you could switch to the "manual" method.  This will not remove
  412. the defined folds.  Then you can delete or add folds manually.
  413.  
  414. ==============================================================================
  415.  
  416. Next chapter: |usr_29.txt|  Moving through programs
  417.  
  418. Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl:
  419.